import { Command, Extension, RawCommands, SelectorResult } from "@valuation/core";
import { PluginKey,PatchState } from "@rivet/state";
import { Node } from "@rivet/model";
import { Step } from "@rivet/transform";
//定义扩展的命令 ts 自动识别
declare module "@valuation/core" {
  export interface Commands {
    hello: {
      hello: (id: string) => Command;
    };
    hello1: {
      hello1: (id: string) => Command;
    };
  }
}
declare module "@valuation/core" {
  export interface Selectors {
    selectQd: {
      selectQd: (qd: string) => SelectorResult;
    };
    selectDw:{
      selectDw: () => SelectorResult;
    }
  }
}
//command 写法
const hello1: RawCommands["hello"] = (id: string) => (_props: any) => {
  console.log(id);
};
export const historyKey = new PluginKey("historyKey");
interface HistoryState {
  steps: Step[];
}
export const HistoryExtension = Extension.create<
  Record<string, unknown>,
  HistoryState
>({
  name: "history",
  addCommands() {
    return {
      hello1,
      //command 写法
      hello: (id: string) => (_props: any) => {
        console.log(id);
      },
    };
  },
  //有记忆的函数 参数相同的情况下会缓存之前的数据 最大缓存个数是10个
  addSelectors() {
    return {
      //第一中写法 底层实现是基于 reselect 所以可以前置基础输入函数
      selectQd: (qd: string) =>{
        return {
          dependency:[(state: PatchState)=>state.doc],
          selector: (doc: Node) => {
            console.log(qd);
            return qd == "qd" ? doc.content.content : doc.content;
          }
        }
      },
      //第二中写法 可以直接返回一个函数  默认基础函数是 (state: PatchState)=>state.doc
      selectDw: () =>{
        return (doc: Node) => {
          return doc.content;
        }
      },
    };
  },
  addStorage() {
    return {
      steps: [],
    };
  },
  addOptions() {
    return {};
  },
  addGlobalAttributes() {
    return [
      {
        types: ["dw", "qd", "de"],
        attributes: {
          age: {
            default: 10,
          },
        },
      },
    ];
  },
});
